/**
 * 发表个性签名
 */
function Feed(){
	
	this.publishFeed=function(){
		var feedTA = document.getElementById("feedTA");
		//trim()在base.js里定义
		var content = feedTA.value.trim();//去除开头和结尾的空格
		if(content=="" || content.length<=0){
			alert("不能为空");
			return;
		}
		
		//发起异步请求
		$.ajax({
			url:base_domain+'/feed/publish.html',
			data:'content='+content,
			type:'post',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success:function(result){
				if(result>0){
					var lastLog = document.getElementById("lastLog");
					lastLog.innerHTML = "\""+strPagination(content, 30)+"\"";
					feedTA.value="";
					
					//更新发表的时间
					var lastLogTime = document.getElementById("lastLogTime");
					var today = new Date();
					lastLogTime.innerHTML=" 今天   "+today.toLocaleTimeString();
					
					feed.getMyFeeds('1');
				}
				else{
					alert("发布状态出错");
				}
			},
			
			error:function(){
				alert("连接服务器出错");
			}
		});

	};
	
	
	/*
	 * 得到最近一次发布的个性签名
	 */
	this.getLastFeed=function(){
		//个性状态
		var lastLog = document.getElementById("lastLog");
		//更新发表的时间
		var lastLogTime = document.getElementById("lastLogTime");
		
		//发起异步请求
		$.ajax({
			url:base_domain+'/feed/getLastFeed.html',
			type:'get',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success:function(result){
				if(result==null || result==undefined){
					lastLog.innerHTML=" \"您未发表任何状态\"       ";
					lastLogTime.innerHTML="";
				}
				else{
					var content=result.content.trim();
					var createtime=result.createtime;
					
					createtime = new Date(createtime).toLocaleTimeString();
					
					lastLog.innerHTML="\""+strPagination(content,30);+"\"";
					lastLogTime.innerHTML=createtime;
				}

			},
			
			error:function(){
				alert("状态同步出错");
			}
		});
	};
	
	/**
	 * 得到自己过去发表的状态
	 */
	this.getMyFeeds=function(currentPage){
		var friendLogs = document.getElementById("friendLogs");
		var myLogs = document.getElementById("myLogs");
		var atMeLogs = document.getElementById("atMeLogs");
		
		friendLogs.visibility="hidden";
		myLogs.visibility="visible";
		atMeLogs.visibility="hidden";
		
		//发起异步请求
		$.ajax({
			url:base_domain+'/feed/getMyFeeds.html',
			data:'currentPage='+currentPage,
			type:'get',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success:function(result){
				if(result==null || result==undefined || result.length==0){
					myLogs.innerHTML=" \"您未发表任何状态\"       ";
				}
				else{
					var html = "";
					for(var i=0;i<result.length;i++){
						var log = result[i];
						var content=log.content.trim();
						var createtime=log.createtime;
						
						createtime = new Date(createtime).toLocaleTimeString();
						html += i+".  ";
						
						content = strPagination(content,30);
												
						html += content+"    ";
						//添加发表时间
						html += "<font size='2' color='#006677'>"+createtime+"</font>&nbsp;&nbsp";
						
						//添加回复按键
						html += "<input type='button' id='button_"+log.id+"' value='评论' onclick='feed.showFeedComments("+log.id+",1)'>";
						
						html += "<input type='button' id='hide_button_"+log.id+"' style='visibility:hidden;color:#991111;'  value='收起评论' onclick='feed.hideFeedComments("+log.id+")'>";
						
						//添加回复框DIV
						html += "<div id='feedCommentDiv_"+log.id+"' style='width:50%;height:10%;color:#77aa44;display:none;'"+">正在加载评论，请稍候....</div>";
						html +="<br>";
						
						myLogs.innerHTML = html;
					}
				}

			},
			
			error:function(){
				alert("状态同步出错!请稍候再试!");
			}
		});
	};
	
	/**
	 * 显示某一条feed下的评论
	 */
	this.showFeedComments=function(feedId,currentPage){
		var feedCommentDiv = document.getElementById("feedCommentDiv_"+feedId);
		var button = document.getElementById("button_"+feedId);
		var hide_button = document.getElementById("hide_button_"+feedId);
		
		//发起异步请求
		$.ajax({
			url:base_domain+'/feed/getFeedComments.html',
			data:'feedId='+feedId+'&currentPage='+currentPage,
			type:'get',
			dataType : 'json',
			contentType : "application/x-www-form-urlencoded;charset=utf-8",
			success:function(result){
				if(result==null || result==undefined || result.length==0){
					feedCommentDiv.innerHTML=" \"暂时没有其他评论\"       ";
				}
				else{
					var html="";
					for(var i=0;i<result.length;i++){
						var comment = result[i];
						var commentorName = comment.commentorName;//留下评论的人
						var feedBackToUserName = comment.feedBackToUserName;//回复给谁
						var content=comment.content.trim();
						var createtime=comment.createtime;
						
						createtime = new Date(createtime).toLocaleTimeString();
						html += commentorName+" 回复 "+feedBackToUserName+":";
						html += strPagination(content, 25);
						html += "				"+createtime;
						html +="<br>";
					}
					feedCommentDiv.innerHTML = html;
				}
				feedCommentDiv.style.display="block";
				hide_button.style.visibility="visible";
				button.style.visibility="hidden";
			},
			
			error:function(){
				alert("评论加载出错");
			}
		});	
	};
	
	/**
	 * 隐藏评论div
	 */
	this.hideFeedComments = function(feedId){
		var feedCommentDiv = document.getElementById("feedCommentDiv_"+feedId);
		var button = document.getElementById("button_"+feedId);
		var hide_button = document.getElementById("hide_button_"+feedId);
		
		hide_button.style.visibility="hidden";
		button.style.visibility="visible";
		feedCommentDiv.style.display="none";
	};

}



var feed = new Feed();